#include "animation.h"
#include "defs.h"
#include "draw.h"
#include "font.h"
#include "init.h"
#include "input.h"
#include "main.h"
#include "map.h"
#include "sounds.h"
#include "menu.h"
#include "player.h"
#include "monster.h"
#include "structs.h"
#include "plateformes.h"

   void drawImage(SDL_Surface *image, int x, int y)
  {
       SDL_Rect dest;

       dest.x = x;
       dest.y = y;
       dest.w = image->w;
       dest.h = image->h;

       SDL_BlitSurface(image, NULL, jeu.screen, &dest);
   }

  void drawTile(SDL_Surface *image, int destx, int desty, int srcx, int srcy)
  {

    SDL_Rect dest;

    dest.x = destx;
    dest.y = desty;
    dest.w = TILE_SIZE;
    dest.h = TILE_SIZE;


    SDL_Rect src;

    src.x = srcx;
    src.y = srcy;
    src.w = TILE_SIZE;
    src.h = TILE_SIZE;


    SDL_BlitSurface(image, &src, jeu.screen, &dest);

  }

  void draw(void)
  {
   int i ;
    drawImage(map.background, 0, 0);
     drawMap();

    drawAnimatedEntity(&player);

    for(i = 0 ; i < jeu.nombreMonstres ; i++)
    {
        drawAnimatedEntity(&monster[i]);
    }
    drawPlateforme();

   drawHud();

    if (jeu.key1 == 1)
         drawImage (jeu.HUD_key1,20,70);
         if (jeu.key2== 1)
            drawImage(jeu.HUD_key2,40,70);
         if (jeu.key3 == 1)
            drawImage(jeu.HUD_key3,60,70);

    SDL_Flip(jeu.screen);

    SDL_Delay(1);

  }


   SDL_Surface *loadImage(char *name)
   {


       SDL_Surface *temp = IMG_Load(name);
       SDL_Surface *image;

      if (temp == NULL)
      {
           printf("Failed to load image %s\n", name);

          return NULL;
       }

       SDL_SetColorKey(temp, (SDL_SRCCOLORKEY | SDL_RLEACCEL), SDL_MapRGB(temp->format,
                                                   TRANS_R, TRANS_G,TRANS_B));


       image = SDL_DisplayFormat(temp);

       SDL_FreeSurface(temp);

       if (image == NULL)
       {
           printf("Failed to convert image %s to native format\n", name);

           return NULL;
       }


       return image;


   }



  void delay(unsigned int frameLimit)
  {

    unsigned int ticks = SDL_GetTicks();

    if (frameLimit < ticks)
    {
        return;
    }

    if (frameLimit > ticks + 16)
    {
        SDL_Delay(16);
    }

    else
    {
        SDL_Delay(frameLimit - ticks);
    }
  }
  void drawHud(void)
{
       const SDL_VideoInfo* myPointer = SDL_GetVideoInfo();
  /*  myPointer->current_h
    myPointer->current_w*/

    char text[200];

    drawImage(jeu.HUD_vie,(myPointer->current_w - 100),(myPointer->current_h - 70));
    sprintf(text, "%d", jeu.vies);
    drawString(text, (myPointer->current_w - 30),(myPointer->current_h - 57), font);

    drawImage(jeu.HUD_etoiles, 32, 32);
    sprintf(text, "%d", jeu.etoiles);
    drawString(text, 70, 35, font);


}